package cn.xinzhi.servlet;

import cn.xinzhi.Util.KeyDaoUtil;
import cn.xinzhi.dao.Impl.Yh_loginDaoImpl;
import cn.xinzhi.entity.Yh_login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "NewloginServlet")
public class NewloginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html,charaset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        String user = request.getParameter("username");
        String pwd = request.getParameter("password");
        String pwdjm1=new KeyDaoUtil(pwd).get32();

        Yh_loginDaoImpl udi = new Yh_loginDaoImpl();
        Yh_login ui = udi.login(user, pwdjm1);
        if (ui != null) {
            Cookie cookie1 = new Cookie("user", "");
            Cookie cookie2 = new Cookie("pwd", "");

            cookie1.setValue(user);
            cookie2.setValue(pwd);

            response.addCookie(cookie1);
            response.addCookie(cookie2);

            response.sendRedirect("index.jsp");
        } else {
            response.sendRedirect("login.jsp?flag=0");
        }
        out.flush();
        out.close();
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
